package com.softwarerusher.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.softwarerusher.daoImpl.BookDAO;
import com.softwarerusher.datastructure.Cart;
import com.softwarerusher.model.TBook;

public class netshopOrder  extends ActionSupport implements SessionAware{

	String bookisbn;
	int num;
	private Map<String,Object> session;
	
	public String order()
	{
		System.out.print(bookisbn);
		System.out.print(num);
		boolean flag = false;
		if("true".equals(session.get("login")))
		{
			List<Cart> cart = (List<Cart>) session.get("cart");
			for(int i = 0;i<cart.size(); i++)
			{
				if(cart.get(i).getBook().getBookIsbn().equals(bookisbn))
					{
					   cart.get(i).setNum(cart.get(i).getNum()+num);
					   flag = true;
					   session.put("cart", cart);
					   return "success";
					}
					
			}
			
			TBook book = BookDAO.queryByISBN(bookisbn);
			cart.add(new Cart(num,book));
			session.put("cart", cart);
			return "success";
		}
		else
	       return "login";
	    
	}
	public String delete()
	{
	   return "success";
	}
	public String reset()
	{
		return "success";
	}
	
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

	public String getBookisbn() {
		return bookisbn;
	}

	public void setBookisbn(String bookisbn) {
		this.bookisbn = bookisbn;
	}

	public Map<String, Object> getSession() {
		return session;
	}
}
